﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using Facade;
using IronOne.SalesOne.BusinessObjects;

namespace IronOne.SalesOne.SalesOneWeb.UserManagement
{
    public partial class ManageCodeType : SOSecurityPage
   
    {

        private String action = "create";
        private static int codeIdd = 0;
    
        
        protected void Page_Load(object sender, EventArgs e)
        {

            lblMsg.Text = "";

            if (!Page.IsPostBack)
            {
                ValidateUserCredentials(Request.Url.LocalPath);

                if (!String.IsNullOrEmpty(Request.QueryString["a"]))
                {
                    
                    action = Request.QueryString["a"].ToString();

                }

                if (action == "u")
                {
                    SaveButton.Text = "Update";
                    this.LoadData();
                    FunctionLabel.Text = "Update " + Request.QueryString["tt"];
                    
                }
                else if (action == "c")
                
                {
                    SaveButton.Text = "Create";
                    FunctionLabel.Text = "Create " + Request.QueryString["t"] ;
                    
                }
            }

        }

        private void LoadData() {

            try
            {
                int id = Convert.ToInt32(Session["codeId"].ToString());

                CodeFacade fac = new CodeFacade();
                string codeType = Session["selectValue"].ToString();


                switch (codeType)
                {
                    case "Country":
                        Country country = fac.GetCountry(id);
                        codeIdd = country.CodeId;
                        CodeTextBox.Text = country.Code;
                        DescriptionTextBox.Text = country.Description;
                        break;
                    case "Month":
                        Month month = fac.GetMonth(id);
                        codeIdd = month.CodeId;
                        CodeTextBox.Text = month.Code;
                        DescriptionTextBox.Text = month.Description;
                        break;
                    case "ClaimStatus":
                        ClaimStatus claimStatus = fac.GetClaimStatus(id);
                        codeIdd = claimStatus.CodeId;
                        CodeTextBox.Text = claimStatus.Code;
                        DescriptionTextBox.Text = claimStatus.Description;
                        break;
                    case "CompaintSubType":
                        CompaintSubType compaintSubType = fac.GetCompaintSubType(id);
                        codeIdd = compaintSubType.CodeId;
                        CodeTextBox.Text = Convert.ToString(compaintSubType.Code);
                        DescriptionTextBox.Text = compaintSubType.Description;
                        break;
                    case "CompetionType":
                        CompetionType competionType = fac.GetCompetionType(id);
                        codeIdd = competionType.CodeId;
                        CodeTextBox.Text = competionType.Code;
                        DescriptionTextBox.Text = competionType.Description;
                        break;
                    case "ComplaintStatus":
                        ComplaintStatus complaintStatus = fac.GetComplaintStatus(id);
                        codeIdd = complaintStatus.CodeId;
                        CodeTextBox.Text = complaintStatus.Code;
                        DescriptionTextBox.Text = complaintStatus.Description;
                        break;
                    case "ComplaintType":
                        ComplaintType complaintType = fac.GetComplaintType(id);
                        codeIdd = complaintType.CodeId;
                        CodeTextBox.Text = complaintType.Code;
                        DescriptionTextBox.Text = complaintType.Description;
                        break;
                    case "IDType":
                        IDType idType = fac.GetIDType(id);
                        codeIdd = idType.CodeId;
                        CodeTextBox.Text = idType.Code;
                        DescriptionTextBox.Text = idType.Description;
                        break;
                    case "InformationType":
                        InformationType informationType = fac.GetInformationType(id);
                        codeIdd = informationType.CodeId;
                        CodeTextBox.Text = informationType.Code;
                        DescriptionTextBox.Text = informationType.Description;
                        break;
                    case "MaritalStatus":
                        MaritalStatus maritalStatus = fac.GetMaritalStatus(id);
                        codeIdd = maritalStatus.CodeId;
                        CodeTextBox.Text = maritalStatus.Code;
                        DescriptionTextBox.Text = maritalStatus.Description;
                        break;
                    case "PolicyType":
                        PolicyType policyType = fac.GetPolicyType(id);
                        codeIdd = policyType.CodeId;
                        CodeTextBox.Text = policyType.Code;
                        DescriptionTextBox.Text = policyType.Description;
                        break;
                    case "Salutation":
                        Salutation salutation = fac.GetSalutation(id);
                        codeIdd = salutation.CodeId;
                        CodeTextBox.Text = salutation.Code;
                        DescriptionTextBox.Text = salutation.Description;
                        break;
                    case "Measure":
                        Measure measure = fac.GetMeasure(id);
                        codeIdd = measure.CodeId;
                        CodeTextBox.Text = measure.Code;
                        DescriptionTextBox.Text = measure.Description;
                        break;
                    case "PaymentMode":
                        PaymentMode paymentMode = fac.GetPaymentMode(id);
                        codeIdd = paymentMode.CodeId;
                        CodeTextBox.Text = paymentMode.Code;
                        DescriptionTextBox.Text = paymentMode.Description;
                        break;
                    case "PendingRequirement":
                        PendingRequirement pendingRequirement = fac.GetPendingRequirement(id);
                        codeIdd = pendingRequirement.CodeId;
                        CodeTextBox.Text = pendingRequirement.Code;
                        DescriptionTextBox.Text = pendingRequirement.Description;
                        break;
                    case "PendingStatus":
                        PendingStatus pendingStatus = fac.GetPendingStatus(id);
                        codeIdd = pendingStatus.CodeId;
                        CodeTextBox.Text = pendingStatus.Code;
                        DescriptionTextBox.Text = pendingStatus.Description;
                        break;
                    case "PolicyStatus":
                        PolicyStatus policyStatus = fac.GetPolicyStatus(id);
                        codeIdd = policyStatus.CodeId;
                        CodeTextBox.Text = policyStatus.Code;
                        DescriptionTextBox.Text = policyStatus.Description;
                        break;
                    case "SOType":
                        SOType soType = fac.GetSOType(id);
                        codeIdd = soType.CodeId;
                        CodeTextBox.Text = soType.Code;
                        DescriptionTextBox.Text = soType.Description;
                        break;

                    default:
                        break;

                }
                
            }
            catch (Exception ex)
            {
                return;
            }
        
        }

        private void Update() {

            String selectedTable = Request.QueryString["tt"];

            

            if (selectedTable == "Country")
            {
                
                    Country codecountry = new Country();
                    codecountry.Code = CodeTextBox.Text;
                    codecountry.Description = DescriptionTextBox.Text;
                    codecountry.CodeId = codeIdd;

                    CodeFacade codefacade = new CodeFacade();
                    int successUpdate = codefacade.Updatecountry(codecountry);

                    if (successUpdate == 1)
                    {
                        Session["successCodeUpdate"] = "Successfully updated. ";
                    }
                    else
                    {
                        Session["failCodeUpdate"] = "Failed to update. ";
                    }

            }
            else if (selectedTable == "ClaimStatus")
            {
                ClaimStatus codeClaim = new ClaimStatus();
                codeClaim.Code = CodeTextBox.Text;
                codeClaim.Description = DescriptionTextBox.Text;
                codeClaim.CodeId = codeIdd;

                CodeFacade codefacade = new CodeFacade();
                int successUpdate = codefacade.UpdateClaimStatus(codeClaim);
                if (successUpdate == 1)
                {
                    Session["successCodeUpdate"] = "Successfully updated. ";
                }
                else
                {
                    Session["failCodeUpdate"] = "Failed to update. ";
                }
            }
            else if (selectedTable == "CompaintSubType")
            {
                CompaintSubType CompaintSubType = new CompaintSubType();
                CompaintSubType.Code = CodeTextBox.Text;
                CompaintSubType.Description = DescriptionTextBox.Text;
                CompaintSubType.CodeId = codeIdd;
                CodeFacade codefacade = new CodeFacade();
                int successUpdate = codefacade.UpdateCompaintSubType(CompaintSubType);
                if (successUpdate == 1)
                {
                    Session["successCodeUpdate"] = "Successfully updated. ";
                }
                else
                {
                    Session["failCodeUpdate"] = "Failed to update. ";
                }
            }
            else if (selectedTable == "CompetionType")
            {
                CompetionType CompetionType = new CompetionType();
                CompetionType.Code = CodeTextBox.Text;
                CompetionType.Description = DescriptionTextBox.Text;
                CompetionType.CodeId = codeIdd;
                CodeFacade codefacade = new CodeFacade();
                int successUpdate = codefacade.UpdateCompetionType(CompetionType);
                if (successUpdate == 1)
                {
                    Session["successCodeUpdate"] = "Successfully updated. ";
                }
                else
                {
                    Session["failCodeUpdate"] = "Failed to update. ";
                }
            }
            else if (selectedTable == "ComplaintType")
            {
                ComplaintType ComplaintType = new ComplaintType();
                ComplaintType.Code = CodeTextBox.Text;
                ComplaintType.Description = DescriptionTextBox.Text;
                ComplaintType.CodeId = codeIdd;
                CodeFacade codefacade = new CodeFacade();
                int successUpdate = codefacade.UpdateComplaintType(ComplaintType);
                if (successUpdate == 1)
                {
                    Session["successCodeUpdate"] = "Successfully updated. ";
                }
                else
                {
                    Session["failCodeUpdate"] = "Failed to update. ";
                }
            }
            else if (selectedTable == "ComplaintStatus")
            {
                ComplaintStatus ComplaintStatus = new ComplaintStatus();
                ComplaintStatus.Code = CodeTextBox.Text;
                ComplaintStatus.Description = DescriptionTextBox.Text;
                ComplaintStatus.CodeId = codeIdd;
                CodeFacade codefacade = new CodeFacade();
                int successUpdate = codefacade.UpdateComplaintStatus(ComplaintStatus);
                if (successUpdate == 1)
                {
                    Session["successCodeUpdate"] = "Successfully updated. ";
                }
                else
                {
                    Session["failCodeUpdate"] = "Failed to update. ";
                }
            }
            else if (selectedTable == "IDType")
            {
                IDType IDType = new IDType();
                IDType.Code = CodeTextBox.Text;
                IDType.Description = DescriptionTextBox.Text;
                IDType.CodeId = codeIdd;
                CodeFacade codefacade = new CodeFacade();
                int successUpdate = codefacade.UpdateIDType(IDType);
                if (successUpdate == 1)
                {
                    Session["successCodeUpdate"] = "Successfully updated. ";
                }
                else
                {
                    Session["failCodeUpdate"] = "Failed to update. ";
                }
            }
            else if (selectedTable == "InformationType")
            {
                InformationType InformationType = new InformationType();
                InformationType.Code = CodeTextBox.Text;
                InformationType.Description = DescriptionTextBox.Text;
                InformationType.CodeId = codeIdd;
                CodeFacade codefacade = new CodeFacade();
                int successUpdate = codefacade.UpdateInformationType(InformationType);
                if (successUpdate == 1)
                {
                    Session["successCodeUpdate"] = "Successfully updated.";
                }
                else
                {
                    Session["failCodeUpdate"] = "Failed to update. ";
                }
            }
            else if (selectedTable == "MaritalStatus")
            {
                MaritalStatus MaritalStatus = new MaritalStatus();
                MaritalStatus.Code = CodeTextBox.Text;
                MaritalStatus.Description = DescriptionTextBox.Text;
                MaritalStatus.CodeId = codeIdd;
                CodeFacade codefacade = new CodeFacade();
                int successUpdate = codefacade.UpdateMaritalStatus(MaritalStatus);
                if (successUpdate == 1)
                {
                    Session["successCodeUpdate"] = "Successfully updated. ";
                }
                else
                {
                    Session["failCodeUpdate"] = "Failed to update. ";
                }
            }


            else if (selectedTable == "Month")
            {
                Month Month = new Month();
                Month.Code = CodeTextBox.Text;
                Month.Description = DescriptionTextBox.Text;
                Month.CodeId = codeIdd;
                CodeFacade codefacade = new CodeFacade();
                int successUpdate = codefacade.UpdateMonth(Month);
                if (successUpdate == 1)
                {
                    Session["successCodeUpdate"] = "Successfully updated. ";
                }
                else
                {
                    Session["failCodeUpdate"] = "Failed to update. ";
                }
            }
            else if (selectedTable == "PolicyType")
            {
                PolicyType policyType = new PolicyType();
                policyType.Code = CodeTextBox.Text;
                policyType.Description = DescriptionTextBox.Text;
                policyType.CodeId = codeIdd;
                CodeFacade codefacade = new CodeFacade();
                int successUpdate = codefacade.UpdatePolicyType(policyType);
                if (successUpdate == 1)
                {
                    Session["successCodeUpdate"] = "Successfully updated. ";
                }
                else
                {
                    Session["failCodeUpdate"] = "Failed to update. ";
                }
            }
            else if (selectedTable == "Salutation")
            {
                Salutation salutation = new Salutation();
                salutation.Code = CodeTextBox.Text;
                salutation.Description = DescriptionTextBox.Text;
                salutation.CodeId = codeIdd;
                CodeFacade codefacade = new CodeFacade();
                int successUpdate = codefacade.UpdateSalutation(salutation);
                if (successUpdate == 1)
                {
                    Session["successCodeUpdate"] = "Successfully updated. ";
                }
                else
                {
                    Session["failCodeUpdate"] = "Failed to update. ";
                }
            }
            else if (selectedTable == "Measure")
            {
                Measure measure = new Measure();
                measure.Code = CodeTextBox.Text;
                measure.Description = DescriptionTextBox.Text;
                measure.CodeId = codeIdd;
                CodeFacade codefacade = new CodeFacade();
                int successUpdate = codefacade.UpdateMeasure(measure);
                if (successUpdate == 1)
                {
                    Session["successCodeUpdate"] = "Successfully updated. ";
                }
                else
                {
                    Session["failCodeUpdate"] = "Failed to update. ";
                }
            }
            else if (selectedTable == "PaymentMode")
            {
                PaymentMode paymentMode = new PaymentMode();
                paymentMode.Code = CodeTextBox.Text;
                paymentMode.Description = DescriptionTextBox.Text;
                paymentMode.CodeId = codeIdd;
                CodeFacade codefacade = new CodeFacade();
                int successUpdate = codefacade.UpdatePaymentMode(paymentMode);
                if (successUpdate == 1)
                {
                    Session["successCodeUpdate"] = "Successfully updated. ";
                }
                else
                {
                    Session["failCodeUpdate"] = "Failed to update. ";
                }
            }
            else if (selectedTable == "SOType")
            {
                SOType soType = new SOType();
                soType.Code = CodeTextBox.Text;
                soType.Description = DescriptionTextBox.Text;
                soType.CodeId = codeIdd;
                CodeFacade codefacade = new CodeFacade();
                int successUpdate = codefacade.UpdateSOType(soType);
                if (successUpdate == 1)
                {
                    Session["successCodeUpdate"] = "Successfully updated. ";
                }
                else
                {
                    Session["failCodeUpdate"] = "Failed to update. ";
                }
            }
            else if (selectedTable == "PendingRequirement")
            {
                PendingRequirement pendingRequirement = new PendingRequirement();
                pendingRequirement.Code = CodeTextBox.Text;
                pendingRequirement.Description = DescriptionTextBox.Text;
                pendingRequirement.CodeId = codeIdd;
                CodeFacade codefacade = new CodeFacade();
                int successUpdate = codefacade.UpdatePendingRequirement(pendingRequirement);
                if (successUpdate == 1)
                {
                    Session["successCodeUpdate"] = "Successfully updated. ";
                }
                else
                {
                    Session["failCodeUpdate"] = "Failed to update. ";
                }
            }
            else if (selectedTable == "PendingStatus")
            {
                PendingStatus pendingStatus = new PendingStatus();
                pendingStatus.Code = CodeTextBox.Text;
                pendingStatus.Description = DescriptionTextBox.Text;
                pendingStatus.CodeId = codeIdd;
                CodeFacade codefacade = new CodeFacade();
                int successUpdate = codefacade.UpdatePendingStatus(pendingStatus);
                if (successUpdate == 1)
                {
                    Session["successCodeUpdate"] = "Successfully updated.";
                }
                else
                {
                    Session["failCodeUpdate"] = "Failed to update. ";
                }
            }


            else if (selectedTable == "PolicyStatus")
            {
                PolicyStatus policyStatus = new PolicyStatus();
                policyStatus.Code = CodeTextBox.Text;
                policyStatus.Description = DescriptionTextBox.Text;
                policyStatus.CodeId = codeIdd;
                CodeFacade codefacade = new CodeFacade();
                int successUpdate = codefacade.UpdatePolicyStatus(policyStatus);
                if (successUpdate == 1)
                {
                    Session["successCodeUpdate"] = "Successfully updated. ";
                }
                else
                {
                    Session["failCodeUpdate"] = "Failed to update. ";
                }
            }
        
        }

        private void Create()
        {


            String selectedTable = Request.QueryString["t"];
            
                if (selectedTable == "Country")
                {
                    try
                    {

                        Country codecountry = new Country();
                        codecountry.Code = CodeTextBox.Text;
                        codecountry.Description = DescriptionTextBox.Text;
                        CodeFacade codefacade = new CodeFacade();
                        codefacade.InsertCodeCountry(codecountry); 
                        CodeTextBox.Text = "";
                        DescriptionTextBox.Text = "";
                        lblMsg.Font.Bold = true;
                        lblMsg.ForeColor = System.Drawing.Color.Green;
                        lblMsg.Text = "You have successfully created  " + Request.QueryString["t"] + "."; 
                        //Response.Write("<script>alert('You have successfully created a ' + '" + selectedTable + "')</script>");
                        
                        

                    }catch(Exception ex){
                        lblMsg.Font.Bold = true;
                        lblMsg.ForeColor = System.Drawing.Color.Red;
                        lblMsg.Text = "Cannot insert the code value for the " + selectedTable + ".";
                        //Response.Write("<script>alert('Cannot insert the value for the ' + '" + selectedTable + "')</script>");
                    }
                }
                else if (selectedTable == "ClaimStatus")
                {
                    try{
                    ClaimStatus codeClaim = new ClaimStatus();
                    codeClaim.Code = CodeTextBox.Text;
                    codeClaim.Description = DescriptionTextBox.Text;
                    CodeFacade codefacade = new CodeFacade();
                    codefacade.InsertCode(codeClaim);
                    CodeTextBox.Text = "";
                    DescriptionTextBox.Text = "";
                    lblMsg.Font.Bold = true;
                    lblMsg.ForeColor = System.Drawing.Color.Green;
                    lblMsg.Text = "You have successfully created  " + Request.QueryString["t"]+"."; 
                    //Response.Write("<script>alert('You have successfully created a ' + '" + selectedTable + "')</script>");

                    }
                                                
                    catch (Exception ex)
                    {
                        lblMsg.Font.Bold = true;
                        lblMsg.ForeColor = System.Drawing.Color.Red;
                        lblMsg.Text = "Cannot insert the code value for the " + selectedTable + ".";
                        //Response.Write("<script>alert('Cannot insert the value for the ' + '" + selectedTable + "')</script>");
                        
                    }
                }
                else if (selectedTable == "CompaintSubType")
                {
                    try{
                    CompaintSubType CompaintSubType = new CompaintSubType();
                    CompaintSubType.Code = CodeTextBox.Text;
                    CompaintSubType.Description = DescriptionTextBox.Text;
                    CodeFacade codefacade = new CodeFacade();
                    codefacade.InsertCode(CompaintSubType);
                    CodeTextBox.Text = "";
                    DescriptionTextBox.Text = "";
                    lblMsg.Font.Bold = true;
                    lblMsg.ForeColor = System.Drawing.Color.Green;
                    lblMsg.Text = "You have successfully created  " + Request.QueryString["t"] + ".";
                    //Response.Write("<script>alert('You have successfully created a ' + '" + selectedTable + "')</script>");

                    }
                    catch (Exception ex)
                    {
                        lblMsg.Font.Bold = true;
                        lblMsg.ForeColor = System.Drawing.Color.Red;
                        lblMsg.Text = "Cannot insert the code value for the " + selectedTable + ".";
                        //Response.Write("<script>alert('Cannot insert the value for the ' + '" + selectedTable + "')</script>");
                    }
                }
                else if (selectedTable == "CompetionType")
                {

                    try{
                    CompetionType CompetionType = new CompetionType();
                    CompetionType.Code = CodeTextBox.Text;
                    CompetionType.Description = DescriptionTextBox.Text;
                    CodeFacade codefacade = new CodeFacade();
                    codefacade.InsertCode(CompetionType);
                    CodeTextBox.Text = "";
                    DescriptionTextBox.Text = "";
                    lblMsg.Font.Bold = true;
                    lblMsg.ForeColor = System.Drawing.Color.Green;
                    lblMsg.Text = "You have successfully created  " + Request.QueryString["t"] + "."; 
                    //Response.Write("<script>alert('You have successfully created a ' + '" + selectedTable + "')</script>");

                    }
                    catch (Exception ex)
                    {
                        lblMsg.Font.Bold = true;
                        lblMsg.ForeColor = System.Drawing.Color.Red;
                        lblMsg.Text = "Cannot insert the code value for the " + selectedTable + ".";
                        //Response.Write("<script>alert('Cannot insert the value for the ' + '" + selectedTable + "')</script>");
                    }
                }
                else if (selectedTable == "ComplaintType")
                {

                    try{
                    ComplaintType ComplaintType = new ComplaintType();
                    ComplaintType.Code = CodeTextBox.Text;
                    ComplaintType.Description = DescriptionTextBox.Text;
                    CodeFacade codefacade = new CodeFacade();
                    codefacade.InsertCode(ComplaintType);
                    CodeTextBox.Text = "";
                    DescriptionTextBox.Text = "";
                    lblMsg.Font.Bold = true;
                    lblMsg.ForeColor = System.Drawing.Color.Green;
                    lblMsg.Text = "You have successfully created  " + Request.QueryString["t"] + ".";
                    //Response.Write("<script>alert('You have successfully created a ' + '" + selectedTable + "')</script>");

                    }
                    catch (Exception ex)
                    {
                        lblMsg.Font.Bold = true;
                        lblMsg.ForeColor = System.Drawing.Color.Red;
                        lblMsg.Text = "Cannot insert the code value for the " + selectedTable + ".";
                        //Response.Write("<script>alert('Cannot insert the value for the ' + '" + selectedTable + "')</script>");
                    }
                }
                else if (selectedTable == "ComplaintStatus")
                {

                    try{
                    ComplaintStatus ComplaintStatus = new ComplaintStatus();
                    ComplaintStatus.Code = CodeTextBox.Text;
                    ComplaintStatus.Description = DescriptionTextBox.Text;
                    CodeFacade codefacade = new CodeFacade();
                    codefacade.InsertCode(ComplaintStatus);
                    CodeTextBox.Text = "";
                    DescriptionTextBox.Text = "";
                    lblMsg.Font.Bold = true;
                    lblMsg.ForeColor = System.Drawing.Color.Green;
                    lblMsg.Text = "You have successfully created  " + Request.QueryString["t"] + ".";
                    //Response.Write("<script>alert('You have successfully created a ' + '" + selectedTable + "')</script>");

                    }
                    catch (Exception ex)
                    {
                        lblMsg.Font.Bold = true;
                        lblMsg.ForeColor = System.Drawing.Color.Red;
                        lblMsg.Text = "Cannot insert the code value for the " + selectedTable + ".";
                        //Response.Write("<script>alert('Cannot insert the value for the ' + '" + selectedTable + "')</script>");
                    }
                }
                else if (selectedTable == "IDType")
                {
                    try{
                    IDType IDType = new IDType();
                    IDType.Code = CodeTextBox.Text;
                    IDType.Description = DescriptionTextBox.Text;
                    CodeFacade codefacade = new CodeFacade();
                    codefacade.InsertCode(IDType);
                    CodeTextBox.Text = "";
                    DescriptionTextBox.Text = "";
                    lblMsg.Font.Bold = true;
                    lblMsg.ForeColor = System.Drawing.Color.Green;
                    lblMsg.Text = "You have successfully created  " + Request.QueryString["t"] + ".";
                    //Response.Write("<script>alert('You have successfully created a ' + '" + selectedTable + "')</script>");

                    }
                    catch (Exception ex)
                    {
                        lblMsg.Font.Bold = true;
                        lblMsg.ForeColor = System.Drawing.Color.Red;
                        lblMsg.Text = "Cannot insert the code value for the " + selectedTable + ".";
                        //Response.Write("<script>alert('Cannot insert the value for the ' + '" + selectedTable + "')</script>");
                    }
                }
                else if (selectedTable == "InformationType")
                {
                    try
                    {
                        InformationType InformationType = new InformationType();
                        InformationType.Code = CodeTextBox.Text;
                        InformationType.Description = DescriptionTextBox.Text;
                        CodeFacade codefacade = new CodeFacade();
                        codefacade.InsertCode(InformationType);
                        CodeTextBox.Text = "";
                        DescriptionTextBox.Text = "";
                        lblMsg.Font.Bold = true;
                        lblMsg.ForeColor = System.Drawing.Color.Green;
                        lblMsg.Text = "You have successfully created  " + Request.QueryString["t"] + ".";
                        //Response.Write("<script>alert('You have successfully created a ' + '" + selectedTable + "')</script>");

                    }
                    catch (Exception ex)
                    {
                        lblMsg.Font.Bold = true;
                        lblMsg.ForeColor = System.Drawing.Color.Red;
                        lblMsg.Text = "Cannot insert the code value for the " + selectedTable + ".";
                        //Response.Write("<script>alert('Cannot insert the value for the ' + '" + selectedTable + "')</script>");
                    }
                }
                else if (selectedTable == "MaritalStatus")
                {
                    try{
                    MaritalStatus MaritalStatus = new MaritalStatus();
                    MaritalStatus.Code = CodeTextBox.Text;
                    MaritalStatus.Description = DescriptionTextBox.Text;
                    CodeFacade codefacade = new CodeFacade();
                    codefacade.InsertCode(MaritalStatus);
                    CodeTextBox.Text = "";
                    DescriptionTextBox.Text = "";
                    lblMsg.Font.Bold = true;
                    lblMsg.ForeColor = System.Drawing.Color.Green;
                    lblMsg.Text = "You have successfully created  " + Request.QueryString["t"] + ".";
                    //Response.Write("<script>alert('You have successfully created a ' + '" + selectedTable + "')</script>");


                    }
                    catch (Exception ex)
                    {
                        lblMsg.Font.Bold = true;
                        lblMsg.ForeColor = System.Drawing.Color.Red;
                        lblMsg.Text = "Cannot insert the code value for the " + selectedTable + ".";
                        //Response.Write("<script>alert('Cannot insert the value for the ' + '" + selectedTable + "')</script>");
                    }
                }


                else if (selectedTable == "Month")
                {

                    try{
                    Month Month = new Month();
                    Month.Code = CodeTextBox.Text;
                    Month.Description = DescriptionTextBox.Text;
                    CodeFacade codefacade = new CodeFacade();
                    codefacade.InsertCodeMonth(Month);
                    CodeTextBox.Text = "";
                    DescriptionTextBox.Text = "";
                    lblMsg.Font.Bold = true;
                    lblMsg.ForeColor = System.Drawing.Color.Green;
                    lblMsg.Text = "You have successfully created  " + Request.QueryString["t"] + "."; 
                    //Response.Write("<script>alert('You have successfully created a ' + '" + selectedTable + "')</script>");

                    }
                    catch (Exception ex)
                    {
                        lblMsg.Font.Bold = true;
                        lblMsg.ForeColor = System.Drawing.Color.Red;
                        lblMsg.Text = "Cannot insert the code value for the " + selectedTable + ".";
                        //Response.Write("<script>alert('Cannot insert the value for the ' + '" + selectedTable + "')</script>");
                    }
                }

           
                else if (selectedTable == "PolicyType")
                {

                    try
                    {
                        PolicyType PolicyType = new PolicyType();
                        PolicyType.Code = CodeTextBox.Text;
                        PolicyType.Description = DescriptionTextBox.Text;
                        CodeFacade codefacade = new CodeFacade();
                        codefacade.InsertPolicyType(PolicyType);
                        CodeTextBox.Text = "";
                        DescriptionTextBox.Text = "";
                        lblMsg.Font.Bold = true;
                        lblMsg.ForeColor = System.Drawing.Color.Green;
                        lblMsg.Text = "You have successfully created  " + Request.QueryString["t"] + ".";
                        //Response.Write("<script>alert('You have successfully created a ' + '" + selectedTable + "')</script>");

                    }
                    catch (Exception ex)
                    {
                        lblMsg.Font.Bold = true;
                        lblMsg.ForeColor = System.Drawing.Color.Red;
                        lblMsg.Text = "Cannot insert the code value for the " + selectedTable + ".";
                        //Response.Write("<script>alert('Cannot insert the value for the ' + '" + selectedTable + "')</script>");
                    }
                }

                else if (selectedTable == "Salutation")
                {

                    try
                    {
                        Salutation salutation = new Salutation();
                        salutation.Code = CodeTextBox.Text;
                        salutation.Description = DescriptionTextBox.Text;
                        CodeFacade codefacade = new CodeFacade();
                        codefacade.InsertSalutation(salutation);
                        CodeTextBox.Text = "";
                        DescriptionTextBox.Text = "";
                        lblMsg.Font.Bold = true;
                        lblMsg.ForeColor = System.Drawing.Color.Green;
                        lblMsg.Text = "You have successfully created  " + Request.QueryString["t"] + ".";
                        //Response.Write("<script>alert('You have successfully created a ' + '" + selectedTable + "')</script>");

                    }
                    catch (Exception ex)
                    {
                        lblMsg.Font.Bold = true;
                        lblMsg.ForeColor = System.Drawing.Color.Red;
                        lblMsg.Text = "Cannot insert the code value for the " + selectedTable + ".";
                        //Response.Write("<script>alert('Cannot insert the value for the ' + '" + selectedTable + "')</script>");
                    }
                }

                else if (selectedTable == "Measure")
                {

                    try
                    {
                        Measure measure = new Measure();
                        measure.Code = CodeTextBox.Text;
                        measure.Description = DescriptionTextBox.Text;
                        CodeFacade codefacade = new CodeFacade();
                        codefacade.InsertMeasure(measure);
                        CodeTextBox.Text = "";
                        DescriptionTextBox.Text = "";
                        lblMsg.Font.Bold = true;
                        lblMsg.ForeColor = System.Drawing.Color.Green;
                        lblMsg.Text = "You have successfully created  " + Request.QueryString["t"] + ".";
                        //Response.Write("<script>alert('You have successfully created a ' + '" + selectedTable + "')</script>");

                    }
                    catch (Exception ex)
                    {
                        lblMsg.Font.Bold = true;
                        lblMsg.ForeColor = System.Drawing.Color.Red;
                        lblMsg.Text = "Cannot insert the code value for the " + selectedTable + ".";
                        //Response.Write("<script>alert('Cannot insert the value for the ' + '" + selectedTable + "')</script>");
                    }
                }

                else if (selectedTable == "SOType")
                {

                    try
                    {
                        SOType soType = new SOType();
                        soType.Code = CodeTextBox.Text;
                        soType.Description = DescriptionTextBox.Text;
                        CodeFacade codefacade = new CodeFacade();
                        codefacade.InsertSOType(soType);
                        CodeTextBox.Text = "";
                        DescriptionTextBox.Text = "";
                        lblMsg.Font.Bold = true;
                        lblMsg.ForeColor = System.Drawing.Color.Green;
                        lblMsg.Text = "You have successfully created  " + Request.QueryString["t"] + ".";
                        //Response.Write("<script>alert('You have successfully created a ' + '" + selectedTable + "')</script>");

                    }
                    catch (Exception ex)
                    {
                        lblMsg.Font.Bold = true;
                        lblMsg.ForeColor = System.Drawing.Color.Red;
                        lblMsg.Text = "Cannot insert the code value for the " + selectedTable + ".";
                        //Response.Write("<script>alert('Cannot insert the value for the ' + '" + selectedTable + "')</script>");
                    }
                }

                else if (selectedTable == "PaymentMode")
                {

                    try
                    {
                        PaymentMode paymentMode = new PaymentMode();
                        paymentMode.Code = CodeTextBox.Text;
                        paymentMode.Description = DescriptionTextBox.Text;
                        CodeFacade codefacade = new CodeFacade();
                        codefacade.InsertPaymentMode(paymentMode);
                        CodeTextBox.Text = "";
                        DescriptionTextBox.Text = "";
                        lblMsg.Font.Bold = true;
                        lblMsg.ForeColor = System.Drawing.Color.Green;
                        lblMsg.Text = "You have successfully created  " + Request.QueryString["t"] + ".";
                        //Response.Write("<script>alert('You have successfully created a ' + '" + selectedTable + "')</script>");

                    }
                    catch (Exception ex)
                    {
                        lblMsg.Font.Bold = true;
                        lblMsg.ForeColor = System.Drawing.Color.Red;
                        lblMsg.Text = "Cannot insert the code value for the " + selectedTable + ".";
                        //Response.Write("<script>alert('Cannot insert the value for the ' + '" + selectedTable + "')</script>");
                    }
                }

                else if (selectedTable == "PendingRequirement")
                {

                    try
                    {
                        PendingRequirement pendingRequirement = new PendingRequirement();
                        pendingRequirement.Code = CodeTextBox.Text;
                        pendingRequirement.Description = DescriptionTextBox.Text;
                        CodeFacade codefacade = new CodeFacade();
                        codefacade.InsertPendingRequirement(pendingRequirement);
                        CodeTextBox.Text = "";
                        DescriptionTextBox.Text = "";
                        lblMsg.Font.Bold = true;
                        lblMsg.ForeColor = System.Drawing.Color.Green;
                        lblMsg.Text = "You have successfully created  " + Request.QueryString["t"] + ".";
                        //Response.Write("<script>alert('You have successfully created a ' + '" + selectedTable + "')</script>");

                    }
                    catch (Exception ex)
                    {
                        lblMsg.Font.Bold = true;
                        lblMsg.ForeColor = System.Drawing.Color.Red;
                        lblMsg.Text = "Cannot insert the code value for the " + selectedTable + ".";
                        //Response.Write("<script>alert('Cannot insert the value for the ' + '" + selectedTable + "')</script>");
                    }
                }

                else if (selectedTable == "PendingStatus")
                {

                    try
                    {
                        PendingStatus pendingStatus = new PendingStatus();
                        pendingStatus.Code = CodeTextBox.Text;
                        pendingStatus.Description = DescriptionTextBox.Text;
                        CodeFacade codefacade = new CodeFacade();
                        codefacade.InsertPendingStatus(pendingStatus);
                        CodeTextBox.Text = "";
                        DescriptionTextBox.Text = "";
                        lblMsg.Font.Bold = true;
                        lblMsg.ForeColor = System.Drawing.Color.Green;
                        lblMsg.Text = "You have successfully created  " + Request.QueryString["t"] + ".";
                        //Response.Write("<script>alert('You have successfully created a ' + '" + selectedTable + "')</script>");

                    }
                    catch (Exception ex)
                    {
                        lblMsg.Font.Bold = true;
                        lblMsg.ForeColor = System.Drawing.Color.Red;
                        lblMsg.Text = "Cannot insert the code value for the " + selectedTable + ".";
                        //Response.Write("<script>alert('Cannot insert the value for the ' + '" + selectedTable + "')</script>");
                    }
                }

                else if (selectedTable == "PolicyStatus")
                {

                    try
                    {
                        PolicyStatus policyStatus = new PolicyStatus();
                        policyStatus.Code = CodeTextBox.Text;
                        policyStatus.Description = DescriptionTextBox.Text;
                        CodeFacade codefacade = new CodeFacade();
                        codefacade.InsertPolicyStatus(policyStatus);
                        CodeTextBox.Text = "";
                        DescriptionTextBox.Text = "";
                        lblMsg.Font.Bold = true;
                        lblMsg.ForeColor = System.Drawing.Color.Green;
                        lblMsg.Text = "You have successfully created  " + Request.QueryString["t"] + ".";
                        //Response.Write("<script>alert('You have successfully created a ' + '" + selectedTable + "')</script>");

                    }
                    catch (Exception ex)
                    {
                        lblMsg.Font.Bold = true;
                        lblMsg.ForeColor = System.Drawing.Color.Red;
                        lblMsg.Text = "Cannot insert the code value for the " + selectedTable + ".";
                        //Response.Write("<script>alert('Cannot insert the value for the ' + '" + selectedTable + "')</script>");
                    }
                }

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["a"] == "c"){
            Response.Redirect("~/UserManagement/CodeType.aspx?t=" + Request.QueryString["t"]);
        }
            else if (Request.QueryString["a"] == "u")
            {
                Response.Redirect("~/UserManagement/CodeType.aspx?tt=" + Request.QueryString["tt"]);
        }
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["a"] == "c")
            {
                
                if (CodeTextBox.Text != "" && DescriptionTextBox.Text != "")
                {
                    Create();
                    Session["codeId"] = null;
                }
            }
            else if (Request.QueryString["a"] == "u") {
               
                Update();
                Session["codeId"] = null;
                Response.Redirect("~/UserManagement/CodeType.aspx?tt=" + Request.QueryString["tt"]);
            }

            //Server.Transfer("/UserManagement/ManageCodeTypeCreate.aspx?codeTypeCreate=" + Request.QueryString["codeTypeCreate"]);
           
        }
    }
}
